<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="utf-8">
    <title>车位列表--共享车位管理系统</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="format-detection" content="telephone=no">
    <link rel="stylesheet" href="resources/layui/css/layui.css" media="all"/>
    <link rel="stylesheet" href="resources/css/public.css" media="all"/>
</head>
<body class="childrenBody">
<form class="layui-form">
    <blockquote class="layui-elem-quote quoteBox">
        <form class="layui-form">
            <div class="layui-inline">
                <div class="layui-input-inline">
                    <input type="text" class="layui-input searchVal" placeholder="请输入搜索车位号"/>
                </div>
                <div class="layui-input-inline">
                    <input type="text" class="layui-input searchVal1" placeholder="请输入搜索车位地址"/>
                </div>
                <a class="layui-btn search_btn" data-type="reload">搜索</a>
            </div>
            <div style="float: right"><input type="button" value="发布车位" class="layui-btn" id="fbcw"></div>
        </form>
    </blockquote>
    <table id="carsparking" lay-filter="carsparkingList"></table>
    <!--操作-->
    <script type="text/html" id="carsparkingBar">
        <a class="layui-btn layui-btn-xs" lay-event="edit">详情</a>
        <a class="layui-btn layui-btn-xs" lay-event="shezhi">更改锁定状态</a>
    </script>
</form>
<script type="text/javascript" src="resources/layui/layui.js"></script>
<script th:inline="none">
    layui.use(['form', 'layer', 'table', 'laytpl'], function () {
        var form = layui.form,
            layer = parent.layer === undefined ? layui.layer : top.layer,
            $ = layui.jquery,
            laytpl = layui.laytpl,
            table = layui.table;
        $("#fbcw").click(function () {
            var index=layui.layer.open({
                title: "添加车位",
                type: 2,
                content: "toaddcarparking.do",
                area: ['500px', '360px'],
                success:function () {
                    var body = layui.layer.getChildFrame('body', index);
                }
            })
        })
        //停车位表
        var tableIns = table.render({
            elem: '#carsparking',
            url: 'cars.do',
            cellMinWidth: 95,
            page: true,
            height: "full-200",
            limits: [10, 15, 20, 25],
            limit: 10,
            id: "carsparkingListTable",
            cols: [[
                {field: 'id', title: 'id', minWidth: 100, align: "center"},
                {field: 'address', title: '地址', align: 'center'},
                {field: 'price', title: '单价', align: 'center'},
                {field: 'state', title: '租用状态', align: 'center' ,templet: function (obj) {
                        return obj.state == 1 ? "已租" : "未租";
                    }},
                {field: 'uState', title: '锁定状态', align: 'center' ,templet: function (obj) {
                        return obj.uState == 1 ? "已锁定" : "未锁定";
                    }},
                {title: '操作', width: 260, templet: '#carsparkingBar', fixed: "right", align: "center"}
            ]]
        });
        //实现搜索功能
        $(".search_btn").on("click",function () {
            var id=$(".searchVal").val()
            var address=$(".searchVal1").val()
            table.reload("carsparkingListTable",{
                page:{
                    curr:1
                },
                where: {
                    id:id,
                    address:address,
                }
            })
        })



        // tool(xx)这里的xx是和上边表个的lay-filter="userList"对应的
        table.on("tool(carsparkingList)", function (obj) {
            //获取当前行的数据
            var data = obj.data;
            switch (obj.event) {
                case 'edit':
                    editCar(data);
                    break;
                case 'shezhi':
                    change(data);
            }
        });
        //修改状态
      function change(data) {
          var item=   data.uState;
          if(item==0){
              item=1
          }else {
              item=0
          }
          if(data.state==1){
              layer.msg("该车位正在出租不能修改状态")
          }else {
              $.ajax({
                  url:'setustate',
                  type:'put',
                  data:{id:data.id,uState:item},
                  success:function (res) {
                      if(res.code==200){
                          layer.msg(res.msg)
                          window.location = "carparking.do"
                      }else {
                          layer.msg(res.msg)
                          parent.location.reload(carparking.do);
                      }
                  }
              })
          }

      }
     function editCar(data) {
         var index=layui.layer.open({
             title: "详细信息",
             type: 2,
             content: "toeditCarpage.do",
             area: ['500px', '360px'],
             success:function (res) {
                 var body = layui.layer.getChildFrame('body', index);
                 body.contents().find(".id").val(data.id)
                 body.contents().find(".address").val(data.address)
                 body.contents().find(".price").val(data.price)
                 if(data.state==1){
                     body.contents().find(".state").val("已租")
                 }else {
                     body.contents().find(".state").val("未租")
                 }
             }
         })
     }

    });


</script>
</body>
</html>